package cn.bob.org.controller;


import cn.bob.basic.query.DeptQuery;
import cn.bob.basic.utils.AjaxResult;
import cn.bob.basic.utils.PageList;
import cn.bob.org.domain.Dept;
import cn.bob.org.domain.PowerId;
import cn.bob.org.domain.User;
import cn.bob.org.service.DeptService;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.apache.ibatis.javassist.compiler.ast.Keyword;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/dept")
public class DeptController {
    @Autowired
    private DeptService deptService;

    // 添加或修改
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Dept dept){
        System.out.println(dept);
        if (dept.getDeptId()==null){
            deptService.addDeept(dept);
        }else {

            deptService.updateDept(dept);
        }
        return AjaxResult.me().success();
    }
    // 查询所有
    @GetMapping
    public List<Dept> findAll(){
       return deptService.findAll();
    }
    // 删除
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        deptService.delDept(id);
        return AjaxResult.me().success();
    }
    // 单条语句查询
    @GetMapping("/{id}")
    public  Dept findById(@PathVariable("id") Long id){
        return deptService.findByDeptId(id);
    }
    // 分页查询
    @PostMapping
    public PageList<Dept> queryByPage(@RequestBody DeptQuery query){
        //System.out.println(query);
        return deptService.queeryByPage(query);
    }
    // 测试
    @GetMapping("/test")
    public Integer findByUser(@RequestParam String name){
        //System.out.println("登录中的用户:"+name);
        //System.out.println(deptService.findByUser(name).getPowerId());
        return deptService.findByUser(name).getPowerId();
    }
}
